package io.github.libkodi.basic_gateway.component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import io.github.libkodi.basic_gateway.tools.ClassUtils;

public class ComponentObject {
	private Class<?> cls;
	private boolean fromComponents = false;
	private Object parentInstance = null;
	private Object instance = null;
	private Method method = null;
	
	public ComponentObject(Class<?> cls) {
		this.cls = cls;
	}
	
	public ComponentObject(Class<?> cls, Object instance, Method method) {
		this.cls = cls;
		this.parentInstance = instance;
		this.method = method;
		fromComponents = true;
	}

	public Class<?> getClassType() {
		return cls;
	}

	public Object getInstance() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		if (instance == null) {
			if (fromComponents) {
				instance = method.invoke(parentInstance);
			} else {
				instance = ClassUtils.newInstance(cls);
			}
		}
		
		return instance;
	}
	
	public boolean isFromComponents() {
		return fromComponents;
	}
}
